// import axios from 'axios';
// const axios = window.$axios

const base = window.api_base;

const userBase = `${base}/index/user`

const defaultParams = {};

function axios(method, url, param) {
  return new Promise(function (resolve, reject) {
    $.ajax({
      method: method,
      url: url,
      data: param,
      xhrFields: {
        withCredentials: true
      },
      success: function (data) {
        resolve(data);
      },
      error: function (error) {
        reject(error)
      }
    });
  })
};


function mixParams(params) {
  for (const k in defaultParams) {
    if (!params.hasOwnProperty(k)) {
      params[k] = defaultParams[k];
    }
  }
  return params;
}

export function Login(params) {
  return axios('post',`${userBase}/login.html`,mixParams(params))
}
export function Logout(params) {
  return axios('post',`${userBase}/logout.html`,mixParams(params))
}

export function getActivity(params) {
  return axios('post',`${userBase}/activity.html`,mixParams(params))
}

/**
 * 
 * @param 获取部门人数
 */
export function getDepartList(params) {
  return axios('post',`${userBase}/department_list.html`,mixParams(params))
}
/* 增加部门 name */
export function addDepartList(params) {
  return axios('post',`${userBase}/department_add.html`,mixParams(params))
}
/*  编辑部门 id,name */
export function editDepartList(params) {
  return axios('post',`${userBase}/department_edit.html`,mixParams(params))
}
/*  删除部门 id */
export function deleteDepartList(params) {
  return axios('post',`${userBase}/department_delete.html`,mixParams(params))
}
/*  禁用部门 id */
export function statusDepartList(params) {
  return axios('post',`${userBase}/department_status.html`,mixParams(params))
}
/**
 * 
 * @param 获取签到人数
 */
export function getSignLog(params) {
  return axios('post',`${userBase}/sign_log.html`,mixParams(params)).then(res=>{
    if (!res.data.code) {
      formatUserList(res.data.list.data);
      console.log(params)
		}
		return res.data;
  })
}
/**
 * 
 * @param 获取用户列表
 */
export function getUserList(params) {
  return axios('post',`${userBase}/user_list.html`,mixParams(params)).then((res)=>{
    if (!res.data.code) {
      formatUserList(res.data.list.data);
      console.log(params)
		}
		return res.data;
  })
}

/**
 * 
 * @param 获取奖项列表 
 */
export function getPrizeList(params) {
  return axios('post',`${userBase}/prize_list.html`,mixParams(params))
}
/**
 * 
 * @param 添加奖项 (name,num,per 名称,抽取人数,每页人数)
 */
export function addPrize(params) {
  return axios('post',`${userBase}/prize_add.html`,mixParams(params))
}
/**
 * 
 * @param 修改奖项 (name,num,per 名称,抽取人数,每页人数)
 */
export function editPrize(params) {
  return axios('post',`${userBase}/prize_edit.html`,mixParams(params))
}
/**
 * 
 * @param 删除奖项 (name,num,per 名称,抽取人数,每页人数)
 */
export function deletePrize(params) {
  return axios('post',`${userBase}/prize_delete.html`,mixParams(params))
}
/**
 * 
 * @param 禁用奖项 (name,num,per 名称,抽取人数,每页人数)
 */
export function statusPrize(params) {
  return axios('post',`${userBase}/prize_status.html`,mixParams(params))
}

/**
 * 
 * @param  奖品列表 
 */
export function getAwardList(params) {
  return axios('post',`${userBase}/award_list.html`,mixParams(params))
}
/** 增加奖项 */
export function addAward(params) {
  return axios('post',`${userBase}/award_add.html`,mixParams(params))
}
/** 修改奖项 */
export function editAward(params) {
  return axios('post',`${userBase}/award_edit.html`,mixParams(params))
}
/** 删除奖项 */
export function deleteAward(params) {
  return axios('post',`${userBase}/award_delete.html`,mixParams(params))
}
/** 设置状态 */
export function statusAward(params) {
  return axios('post',`${userBase}/award_status.html`,mixParams(params))
}
/**
 * 
 *  @param 中奖纪录
 */

export function winningLog(params) {
  return axios('post',`${userBase}/winning_log.html`,mixParams(params)).then((res)=>{
    if (!res.data.code) {
      formatUserList(res.data.list.data);
      console.log(params)
		}
		return res.data;
  })
}
// 清空中奖记录
export function winningEmpty(params) {
  return axios('post',`${userBase}/winning_empty.html`,mixParams(params))
}



export function pingLottory(params) {
  return axios('post',`${userBase}/ping.html`,mixParams(params))
}





export function formatUserList(list) {
	for (const o of list) {
		if (!o.hasOwnProperty('head')) {
			o.head = o.image_head_url;
		}
		if (o.detail) {
			const detail = JSON.parse(o.detail);
			switch (typeof detail) {
				case 'string':
					o.company = detail;
					o.tel = '';
					break;
				case 'object':
					if (Array.isArray(detail)) {
						o.company = detail[0];
						o.tel = detail[1];
					}
					break;
			}
		}
		if (!o.hasOwnProperty('company')) {
			o.company = '';
		}
		if (!o.hasOwnProperty('tel')) {
			o.tel = '';
		}
	}
	return list;
}